package com.ddn.controller.hall;

import com.ddn.model.hall.User;
import com.ddn.model.hall.UserVo;
import com.ddn.service.hall.UserService;
import com.ddn.util.JsonResult;
import com.ddn.util.KaptchaUtil;
import com.ddn.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;

/*
  ❀❀❀---扎根---❀❀❀
    ❤ @libing ❤
  
*/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param req
     * @param loginVo
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public JsonResult<User> login(HttpServletRequest req, UserVo loginVo, HttpSession session){
        //判断

        JsonResult jsonResult= JsonResult.ok();
        if (session.getAttribute("code").toString().equals(loginVo.getCaptcha())){

            //通过用户名和密码去登录
            String username = loginVo.getUsername();
            String password = loginVo.getPassword();

            String salt = "qwer.#";//加盐，还能加散列次数

            //spring帮我们提供相关加密的工具类
            //String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
            password = DigestUtils.md5DigestAsHex((password + salt).getBytes());

            HashMap<String, Object> map = new HashMap<>();
            map.put("username",username);
            map.put("password",password);



            //调佣
            User login =  userService.userLogin(map);

            // 登录成功,,查询到数据,将数据存入session,供后续身份认证
            if(login !=null){
                session = req.getSession();
                session.setAttribute("username","12313");
                System.out.println("登录时："+session);

            }
            jsonResult.setData(login);
            return jsonResult;
        }

        return JsonResult.fail();
    }

    /**
     *  生成验证码
     * @param session
     * @param response
     * @throws IOException
     */
    @RequestMapping("/createCaptcha")
    public void createCaptcha(HttpSession session, HttpServletResponse response) throws IOException {

        KaptchaUtil.createKaptchaImg(session,response);

        System.out.println("验证码结果：" + session.getAttribute("code").toString());

    }

    /**
     * 用户退出
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/loginout")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //把登录信息从session中删除，跳转到登录页面
        //获取session
        //false表示获取session，如果没有就是null，
        //true默认不写，有session获取session，没有session创建session
        HttpSession session = request.getSession(false);

        System.out.println(" 退出前      session  = " + session.toString());

        //移除数据
        //session.removeAttribute("admin");
        //销毁session
        session.invalidate();
        System.out.println(" 退出后     session  = " + session.toString());
    }

    /**
     * 查看用户信息
     * @param map
     * @return
     */
    @RequestMapping("/userInfo")
    public ResultData userInfo(@RequestParam HashMap<String, Object> map) {
        ResultData resultData = userService.userInfo(map);
        return resultData;
    }

    /**
     * 修改用户密码
     * @param map
     * @return
     */
    @RequestMapping("/password")
    public ResultData userPassword(@RequestParam HashMap<String, Object> map) {
        ResultData resultData = userService.userPassword(map);
        return resultData;

    }

}
